#include "project_info_table.h"
#include "sqlite_manager.h"
#include "easylogging++.h"
#include <QDateTime>

ProjectInfoTable::ProjectInfoTable()
{

}

bool ProjectInfoTable::updateProjectBaseInfo(
        const QString &projectUuid,
        const QString &projectName,
        const QString &projectDescription)
{
    SqliteManager sm;

    QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
    bool ret = sm.updateData("prj_info",
                             {
                                 {"prj_name", projectName},
                                 {"prj_description", projectDescription},
                                 {"last_modify_time", currentTime}
                             },
                             {
                                 {"uuid", projectUuid}
                             });
    if (!ret)
    {
        LOG(ERROR) << "update prj info data failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}

bool ProjectInfoTable::updateProjectDescription(const QString &projectUuid, const QString &projectDescription)
{
    SqliteManager sm;
    QList<QString> fields;
    fields << "prj_description";

    QList<QVariantList> data;
    bool ret = sm.updateData("prj_info",
                             {
                                 {"prj_description", projectDescription}
                             },
                             {
                                 {"uuid", projectUuid}
                             });
    if (!ret)
    {
        LOG(ERROR) << "update prj description failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}

bool ProjectInfoTable::getProjectDescription(const QString &projectUuid, QString &description)
{
    SqliteManager sm;
    QList<QString> fields;
    fields << "prj_description";

    QList<QVariantList> data;
    bool ret = sm.selectData("prj_info", fields, {{"uuid", projectUuid}}, data);
    if (!ret)
    {
        LOG(ERROR) << "init prj task data failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    if (data.count() != 0) {
        description = data[0][0].toString();
    }

    return true;
}

bool ProjectInfoTable::getAllProjectInfo(QList<QVariantList> &data)
{
    SqliteManager sm;
    QList<QString> fields;
    fields << "uuid"
           << "prj_name"
           << "prj_description"
           << "create_time"
           << "last_modify_time";
    bool ret = sm.selectData("prj_info",
                             fields, data);
    if (!ret)
    {
        LOG(ERROR) << "get all project info failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}

bool ProjectInfoTable::getProjectInfoOnProjectNameContainsText(QString text, QList<QVariantList> &data)
{
    SqliteManager sm;
    QList<QString> fields;
    fields << "uuid"
           << "prj_name"
           << "prj_description"
           << "create_time"
           << "last_modify_time";
    QString sql = "select uuid, prj_name, prj_description, create_time, last_modify_time from prj_info where prj_name like '%";
    sql += text;
    sql += "%'";
    bool ret = sm.selectDataBySql(sql,
                                 fields, data);
    if (!ret)
    {
        LOG(ERROR) << "get project info on project name contains text failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}
